<?php

class M_berita extends CI_Model {

	function __construct(){
		parent::__construct();
	}

	function berita_terbaru($limit){

		$sql="SELECT berita.id,
					judul,
					dayname(berita.created_date) dayname,
					date_format(berita.created_date,'%d-%m-%Y') tanggal_ina,
					date_format(berita.created_date,'%Y-%m-%d') tanggal,
					permalink,
					isi_berita,
					publish,
					headline,
					tag,
					kategori.kategori,
					image_berita
				FROM berita
				INNER JOIN kategori ON (kategori_id=kategori.id)
				WHERE publish='y'
				ORDER BY berita.id DESC LIMIT $limit ";

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function pengumuman_terbaru($limit){

		$sql="SELECT *,
				date_format(pengumuman.created_date,'%Y-%m-%d') as tanggal
				FROM pengumuman
				WHERE publish='y'
				ORDER BY pengumuman.id DESC LIMIT $limit ";

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function get_view($judul){

		$sql="SELECT *,
				dayname(berita.created_date) dayname,
				date_format(berita.created_date,'%d-%m-%Y') tanggal_ina
				FROM berita
				LEFT JOIN kategori ON (
				berita.kategori_id = kategori.id )
				WHERE lower(judul)='".strtolower($judul)."'";
		$rs=$this->db->query($sql);
		if($rs->num_rows()){
			return $rs->row();
		}else{
			return "";
		}
	}

	function berita_get_count_rows($query){
		$sql="SELECT *
				FROM berita
				LEFT JOIN kategori ON (
				berita.kategori_id = kategori.id )
				WHERE publish='y'";
		$num_rows = $this->m_public_function->get_count_rows($sql);
		return $num_rows;
	}

	function berita_listview($query, $start, $end){

		$sql="SELECT berita.id,
					judul,
					dayname(berita.created_date) dayname,
					date_format(berita.created_date,'%d-%m-%Y') tanggal_ina,
					date_format(berita.created_date,'%Y-%m-%d') tanggal,
					permalink,
					isi_berita,
					headline,
					publish,
					tag,
					kategori.kategori,
					image_berita
				FROM berita
				INNER JOIN kategori ON (kategori_id=kategori.id)
				WHERE publish='y'
				ORDER BY id DESC";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		$sql.=" LIMIT $start, $end";
		//$this->firephp->log($sql);

		$query = $this->db->query($sql);
		if($query->num_rows()>0){
			return $query->result();
		}else{
			return null;
		}

	}

	function getDetail($id){
		$sql = "
			SELECT
				berita.id,
				judul,
				isi_berita,
				kategori_id,
				image_berita,
				publish,
				headline,
				DATE_FORMAT(berita.created_date,'%Y-%m-%d') AS created_date,
				tag,
				kategori.kategori
			FROM berita
			JOIN kategori ON kategori.id = berita.kategori_id
			WHERE berita.id = ".$id."
		";
		$query = $this->db->query($sql);

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function berita_list($search_text, $start, $end, $tglawal, $tglakhir){

		$sql="SELECT
				berita.id, judul, kategori.kategori, headline, publish, DATE_FORMAT(berita.created_date,'%d-%m-%Y') AS created_date, users.username
			FROM
				berita
			LEFT JOIN kategori ON (
				berita.kategori_id = kategori.id
			)
			LEFT JOIN users ON berita.created_by = users.id
			WHERE berita.id IS NOT NULL
			";
		if($search_text != ''){
			$sql .= " 
				AND ( lower(judul) LIKE '%".$search_text."%' 
				OR lower(kategori.kategori) LIKE '%".$search_text."%' 
				OR lower(users.username) LIKE '%".$search_text."%' )
			";
		}
		if($tglawal != '' && $tglakhir != ''){
			$sql .= " AND berita.created_date >= '".$tglawal."' AND berita.created_date <= '".$tglakhir."' ";
		}
		$sql.=" ORDER BY berita.id DESC ";

		$num_rows = $this->m_public_function->get_count_rows($sql);
		if($start != 0 && $end != 0){
			$sql.=" LIMIT $start, $end";
		}

		$query = $this->db->query($sql);

		if($num_rows > 0){
			$result = $query->result();
			$i = 0;
			foreach($result as $row){
				$data[$i] = $row;
				$i++;
			}
		}
		$final[] = array(
			"TotalRows"=>$num_rows,
			"results"=>$data
		);
		return json_encode($final);
	}

	function berita_save($params){
		extract($params);
		if($task == 'create'){
			$this->db->insert('berita',$data);
		}else{
			$this->db->where('id', $id);
			$this->db->update('berita',$data);
		}
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

	function berita_delete($id){
		$this->db->where('id',$id);
		$this->db->delete('berita');
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}
	
	function berita_switchpublish($id, $stats){
		$data = array(
			"publish"=>$stats
		);
		$this->db->where("id", $id);
		$this->db->update('berita', $data);
		if($this->db->affected_rows()){
			return 1;
		}else{
			return 0;
		}
	}

}